package com.activenetwork.smlite.testcase.events.searchEvents;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.activenetwork.smlite.business.EventBusiness;
import com.activenetwork.smlite.page.event.EventsHomeView;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.PageLoader;
import com.activenetwork.util.RandomString;
import com.activenetwork.util.WaitUtil;
import com.activenetwork.util.grid.OperatableGrid;
import com.activenetwork.util.grid.condition.ConditionBuilder;
import com.activenetwork.util.grid.gson.GridFactory;
import com.activenetwork.util.verification.gson.FailurePoint;

public class SearchEvents002 extends BaseCase {
	static final Logger logger = LoggerFactory.getLogger(SearchEvents002.class);
	private String userName;
	private String userPwd;
	private OperatableGrid grid;
	
	@BeforeClass(alwaysRun=true)
	public void dataSetup(){
		userName = "smlite@starcite.com";
		userPwd = "st@rc!te";
		

	}
	
	@Test
	public void  Search_Event_By_Event(){
		loginBusiness.login(userName, userPwd);
		
		// the grid on the home view page
		grid = GridFactory.getGrid("EventsHomeView", "SearchEventGrid");
		
		
		// Verify search with "event", counting how many records
		// then search "EVENT", the counting number should be equal
		EventBusiness eventBusiness = new EventBusiness();
		eventBusiness.searchEvent("event");
		
		ConditionBuilder builder = new ConditionBuilder();
		int num1 = grid.countMatchRow(builder.build());
		
		eventBusiness.searchEvent("EVENT");
		int num2 = grid.countMatchRow(builder.build());
		
		if(num1 != num2){
			EnvContext.failurePointTL.get().add(
					new FailurePoint(SearchEvents002.class.getName(), 
					"Verify search event section is case insensitive", 
					"The result of searching with lowercase and uppercase is different. Not correct."));
		}
		
		// Refresh and verify the pagination
		EventsHomeView eventsHomeView = PageLoader.load(EventsHomeView.class);
		eventsHomeView = (EventsHomeView)eventsHomeView.events();
		WaitUtil waitUtil = new WaitUtil();
		
		int firstPageRowNum = grid.countMatchRow(builder.build());
		if(firstPageRowNum > 25){
			grid.nextPage();
			waitUtil.wait(1000);
			grid.previousPage();
			waitUtil.wait(1000);
			grid.nextPage();
			waitUtil.wait(1000);
			grid.firstPage();
			waitUtil.wait(1000);
		}
		
		// Verify search out no result
		eventsHomeView = (EventsHomeView)eventsHomeView.events();
		String notExistEventTitle = RandomString.generateRandomString(10);
		try{
			eventBusiness.searchEvent(notExistEventTitle,notExistEventTitle);
		}catch(RuntimeException e){
			logger.info("Success for searching with no existing event and no result");
		}
		
		loginBusiness.logout();
		
		
		
	}

}
